"""
@author: 江同学呀
@file: script.py
@date: 2024/11/24 20:48
@desc: 
"""
from numbers import Number
from typing import Dict, Union, Any

from espc.orm.model.base.base import _Base
from espc.orm.model.mapping.field.base_field.base_field import _BaseField


class Script(_Base):
    """
    脚本
    https://www.elastic.co/guide/en/elasticsearch/reference/7.17/modules-scripting.html

    todo
    """
    def __init__(self, source: str, params: Dict[Union[str, _BaseField], Any] = None, lang: str = None, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self._source: str = source
        self._params: Dict[Union[str, _BaseField], Any] = params
        self._lang: str = lang
        return

    def _build(self) -> Dict:
        body: Dict = {"source": self._source}
        if self._params:
            body["params"] = {k.field_name if isinstance(v, _BaseField) else k: v for k, v in self._params.items()}
        if self._lang:
            body["lang"] = self._lang
        return body

